package com.lzhpo.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzhpo.admin.entity.Role;
import com.lzhpo.admin.entity.User;
import com.lzhpo.admin.mapper.UserMapper;
import com.lzhpo.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p> Author：lzhpo </p>
 * <p> Title：</p>
 * <p> Description：</p>
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public User findUserByLoginName(String name) {
        Map<String,Object> map = new HashMap();
        map.put("loginName", name);
        return baseMapper.selectUserByMap(map);
    }

    @Override
    public User findUserById(String id) {
        Map<String,Object> map = new HashMap();
        map.put("id", id);
        return baseMapper.selectUserByMap(map);
    }

    @Override
    public int  userCount(String param) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name",param).or().eq("email",param).or().eq("tel",param);
        int count = baseMapper.selectCount(wrapper);
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUserRoles(String id, Set<Role> roleSet) {
        baseMapper.saveUserRoles(id,roleSet);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUser(User user) {
      //  Encodes.entryptPassword(user);
        user.setLocked(false);
        baseMapper.insert(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUser(User user) {
        dropUserRolesByUserId(user.getId());
        baseMapper.updateById(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteUser(User user) {
        user.setDelFlag(true);
        user.updateById();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void lockUser(User user) {
        user.setLocked(!user.getLocked());
        user.updateById();
    }

    @Override
    public int updatePassWordById(Integer id, String password_old, String password_new) {
        String sql="update sys_user set  password=?  where id =?   and password =?";
        Object [] params=new Object[]{password_new,id,password_old};
        try{
            jdbcTemplate.update(sql,params);
            return  1;
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public int insertNewUser(String password, String user_name,String nick_name) {
        String sql="insert into sys_user(login_name,nick_name,password" +
                ",del_flag)values(?,?,?,false)";
        Object [] params=new Object[]{user_name,nick_name,password};
        try{
            jdbcTemplate.update(sql,params);
            return  1;
        }catch (Exception e){
            e.printStackTrace();
            return -2;
        }

    }

    @Override
    public List<User> findUserList() {
        String sql="SELECT id,login_name,nick_name,password from sys_user order by id asc";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class));
    }

    @Override
    public void deleteUserById(Integer id) {
        jdbcTemplate.update("delete from sys_user where id ="+id);
    }

    @Override
    public void resetPdById(Integer id) {
        jdbcTemplate.update("update  sys_user set  password='123456'  where id = "+id);
    }


    @Transactional(rollbackFor = Exception.class)
    public void dropUserRolesByUserId(String id) {
        baseMapper.dropUserRolesByUserId(id);
    }


}
